/**
 * Copyright (c) 2010-2020 by skzr.org
 * All rights reserved.
 * @Author skzr.org(E-mail:skzr.org@gmail.com)
 * @date 2010-11-11 下午08:37:36
 */
package org.skzr.chart4me.demo;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;

/**
 * 注意：使用?ChartFactory创建的3D的，设置Item Series的Label显示，设置这个还不一定会出来，需要设置位置，如：
 * @jdk 1.6
 * @author skzr.org(E-mail:skzr.org@gmail.com)
 * @version 1.0.0
 */
public class BarChartDemo4My extends ApplicationFrame {
	private static final long serialVersionUID = 1L;

	public BarChartDemo4My(String title) {
		super(title);
		ChartPanel chartPanel = new ChartPanel(createChart());
		chartPanel.setMouseWheelEnabled(true);
		setContentPane(chartPanel);
	}

	private JFreeChart createChart() {
		CategoryDataset dataset = createDataset();
		JFreeChart chart = ChartFactory.createBarChart3D("我的一个BarChart，学生成绩", "学生", "考试成绩", 
				dataset, PlotOrientation.VERTICAL, true, true, false);
		CategoryPlot plot = chart.getCategoryPlot();
		plot.setOutlineVisible(true);
		plot.setDomainGridlinesVisible(true);
		NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
		numberaxis.setUpperMargin(0.25);
		CategoryItemRenderer renderer = plot.getRenderer();
		renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		//注意：使用?ChartFactory创建的3D的，设置这个还不一定会出来，需要设置位置，如：
		ItemLabelPosition position1 = new ItemLabelPosition( ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER);
        renderer.setBasePositiveItemLabelPosition(position1);
        ItemLabelPosition position2 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE6, TextAnchor.TOP_CENTER);
        renderer.setBaseNegativeItemLabelPosition(position2);
		renderer.setSeriesItemLabelsVisible(2, Boolean.TRUE);
		return chart;
	}

	private CategoryDataset createDataset() {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		String[] rowKeys = {"数学", "物理", "化学"};
		String[] columnKeys = {"张三", "李四", "王五"};
		for (String row : rowKeys) {
			for (String col : columnKeys) {
				dataset.addValue(Math.random() * 20 * (Math.random() < 0.4 ? -1 : 1), row, col);
			}
		}
		return dataset;
	}

	public static void main(String[] args) {
		BarChartDemo4My demo = new BarChartDemo4My("我的一个测试");
		demo.pack();
		RefineryUtilities.centerFrameOnScreen(demo);
		demo.setVisible(true);
	}
}
